package com.example.sls.dao.user;

import com.example.sls.pojo.user.Faq;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 常见疑难解答表(Faq)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:54:52
 */
public interface FaqDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from faq where id=#{id}")
  Faq queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param faq      查询条件
   * @param pageSize 分页对象
   * @param offset   偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from faq where 1=1" +
          " and (id=#{faq.id} or (isnull(#{faq.id}) or #{faq.id}=0))" +
          " and (question=#{faq.question} or (isnull(#{faq.question}) or #{faq.question}=''))" +
          " and (answer=#{faq.answer} or (isnull(#{faq.answer}) or #{faq.answer}=''))" +
          " and (created_at=#{faq.createdAt} or (isnull(#{faq.createdAt}) or #{faq.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<Faq> queryAllByLimit(Faq faq, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from faq")
  long count();

  /**
   * 新增数据
   *
   * @param faq 实例对象
   * @return 影响行数
   */
  @Insert("insert into faq (id,question,answer,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{question}!='' and !isnull(#{question}),#{question},null)," +
          "if(#{answer}!='' and !isnull(#{answer}),#{answer},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(Faq faq);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param faq 实例对象
   * @return 影响行数
   */
  @Update("update faq set " +
          "question=if(isnull(#{question}) or #{question}='',question,#{question})," +
          "answer=if(isnull(#{answer}) or #{answer}='',answer,#{answer})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(Faq faq);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from faq where id=#{id}")
  int deleteById(Integer id);
}

